<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/8/24
 * Time: 18:42
 */
class AttachmentService
{
    private $diaryAttachmentModel;

    function __construct()
    {
        $this->diaryAttachmentModel =   new DiaryAttachmentModel();
    }

	public function delDiaryAttachment($bodyId,$uid)
	{
		return $this->diaryAttachmentModel->removeDiaryAttachment(['bodyId'=>$bodyId,'uid'=>$uid]);
	}

    /**
     * 保存点滴、评论、私信附件
     * @param $info
     * type: {1:日记, 2:评论, 3:私信, 4:文章}
     * attachmentType: {1:图片, 2:语音}
     * @param $user
     * @return array
     */
    public function saveDiaryAttachment($info, $user)
    {
        $data  = $this->filterAttachmentReqs($info);
        $attas = $this->mergeAttachments($info);
        $datas = $this->fetchDiaryAttachments($data, $attas, $user);

        return $this->runSaveToDiaryAttachment($user['uid'], $datas);
    }

    /**
     * 过滤附件基本数据(共同点)
     * @param $info
     * @return array
     */
    private function filterAttachmentReqs($info)
    {
        return array(
            'uid'            =>  intval($info['uid']),
            'bodyId'         =>  intval($info['bodyId']),
            'uploadTime'     =>  AC::SERVER_TIME,
            'type'           =>  $info['type'],
            'type_id'        =>  $info['type_id']
        );
    }

    /**
     * 合并附件数据
     * @param $info
     * @return array
     */
    private function mergeAttachments($info)
    {
		$link = $images = $voice = $video = array();
        if (isset($info['attachment']) && is_array($info['attachment']) && $info['attachment'])
        {
            foreach ($info['attachment'] as $val)
            {
                if(is_array($val['info'])) $val['info'] = UK::JsonFormat($val['info']);
                $images[] = array('path' => $val['path'], 'info' => $val['info'], 'attachmentType' => 1);
            }
        }

        if (isset($info['voice']) && is_array($info['voice']) && $info['voice'])
        {
            foreach ($info['voice'] as $val)
            {
                if(is_array($val['info'])) $val['info'] = UK::JsonFormat($val['info']);
                $voice[] = array('path' => $val['path'], 'info' => $val['info'], 'attachmentType' => 2);
            }
        }

        if (isset($info['video']) && is_array($info['video']) && $info['video'])
        {
            foreach ($info['video'] as $val)
            {
                if(is_array($val['info'])) $val['info'] = UK::JsonFormat($val['info']);
                $voice[] = array('path' => $val['path'], 'info' => $val['info'], 'attachmentType' => 3);
            }
        }

		if (isset($info['link']) && is_array($info['link']) && $info['link'])
		{
			foreach ($info['link'] as $val)
			{
				if(is_array($val['info'])) $val['info'] = UK::JsonFormat($val['info']);
				$link[] = array('path' => $val['path'], 'info' => $val['info'], 'attachmentType' => 4);
			}
		}


        $images = array_slice($images, 0, AC::DIARYATTACHMENTLIMIT);
        $voice  = array_slice($voice, 0, AC::DIARYVOICELIMIT);
        $video  = array_slice($video, 0, AC::DIARYVIDEOLIMIT);
		$link   = array_slice($link,0, AC::DIARYLINKLIMIT);
        return array_merge($images, $voice, $video,$link);
    }

    /**
     * 组装，验证附件数据
     * @param $data
     * @param $attachments
     * @param $user
     * @return array
     */
    private function fetchDiaryAttachments($data, $attachments, $user)
    {
        $result = array();
        if ($attachments)
        {
            foreach ($attachments  as  $var)
            {
                $data['attachmentPath'] = is_array($var) ? $var['path'] : $var;
                $data['attachmentType'] = $var['attachmentType'];
                $data['info']           = is_array($var) ? $var['info'] : '';
                if($data['attachmentType']!='4'){
					$check = UK::check_upyun_node($user['uid'], $user['account'], $var['path']) || UK::check_bqyun($var['path']);
					if (!$check) continue;
				}
                $result[] = $data;
            }
        }

        return $result;
    }

    /**
     * 执行数据库存储操作
     * @param $uid
     * @param $datas
     * @return bool
     */
    private function runSaveToDiaryAttachment($uid, $datas)
    {
        if (empty($uid) || empty($datas))
        {
            return false;
        }

        foreach ($datas as $data)
        {
            $this->diaryAttachmentModel->saveAttachment($uid, $data);
        }

        return true;
    }

    /**
     * 将附件数据分类处理
     * @param $param
     * @return array
     */
    public function classifyAttachments($param)
    {
        $link = $images = $voice = $video = array();
        if(isset($param['attachment']) && is_array($param['attachment']) && $param['attachment']){
            foreach ($param['attachment'] as $val)
            {
                if(empty($val['guid'])) {
                    continue;
                }
                switch ($val['type']){
                    case 'image':
                        $images[] = array('path' => $val['src'], 'info' => ['info' => $val['info'],'guid' => $val['guid'],'alt'  => $val['alt']], 'attachmentType' => 1);
                        break;
                    case 'video':
                        $video[] = array('path' => $val['src'], 'info' => ['info' => $val['info'],'guid' => $val['guid'],'alt'  => $val['alt']], 'attachmentType' => 3);
                        break;
                    case 'audio':
                        $voice[] = array('path' => $val['src'], 'info' => ['info' => $val['info'],'guid' => $val['guid'],'alt'  => $val['alt']], 'attachmentType' => 2);
                        break;
                    case 'share':
                        $link[] = array('path' => $val['src'], 'info' => ['info' => $val['info'],'guid' => $val['guid'],'alt'  => $val['alt']], 'attachmentType' => 4);
                        break;
                    default:
                        break;
                }
            }
        }
        unset($param['attachment']);
        $images = array_slice($images, 0, AC::ARTICLEATTACHMENTLIMIT);
        $voice  = array_slice($voice, 0, AC::ARTICLEVOICELIMIT);
        $video  = array_slice($video, 0, AC::ARTICLEVIDEOLIMIT);
        $link   = array_slice($link,0, AC::ARTICLELINKLIMIT);
        if(!empty($images)) $param['attachment'] = $images;
        if(!empty($voice)) $param['voice'] = $voice;
        if(!empty($video)) $param['video'] = $video;
        if(!empty($link)) $param['link'] = $link;
        return $param;
    }

    public function delAttachment($uid, $bodyId, $type){
        return $this->diaryAttachmentModel->delAttachment($uid, $bodyId, $type);
    }

    /**
     * 获取附件
     * @param $uid
     * @param $type
     * @param $typeId
     * @return array|bool
     */
    public function getAttachment($uid, $type, $typeId) {
        $result = $this->diaryAttachmentModel->getAttachment($uid, $type, $typeId);
        return $result;
    }
}